<?

  error_reporting(E_ALL);
  ini_set('display_errors', 1);

include_once 'private.inc.php';
include_once 'controller.inc.php';

$email = $_POST['email'];
$password = $_POST['password'];
$password_check = $_POST['password_check'];
$name = $_POST['name'];
$address = $_POST['address'];
$phone = $_POST['phone'];

$db_link = db_connect();

$accept = TRUE;
$all_users = db_getAllUsers();


foreach ($all_users as $other_user) {
  if ($other_user->getEmail() == $email) {
    $accept = FALSE;
  }
}

if ($accept) {
  $password_hash = encrypt_password($password);
  
  $user = new User();
  $user->setEmail($email);
  $user->setPassword($password_hash);
  $user->setName($name);
  $user->setAddress($address);
  $user->setPhone($phone);
  
  db_createUser($user);
  
  $redirect_to = 'index.php?success=Account_created';
}
else {
  $redirect_to = 'index.php?failure=email';
}

db_disconnect($db_link);
header('Location: '.$redirect_to);

?>
